<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /* 
      bba  ab 
      */
      var minWindow = function (s, t) {
        let missingType = 0
        let minLength = Infinity
        let result = ''
        const map = new Map()
        for (let i = 0; i < t.length; i++) {
          if (!map.has(t[i])) {
            map.set(t[i], 1)
            missingType++
          } else {
            map.set(t[i], map.get(t[i]) + 1)
          }
        }
        let left = 0
        let i = 0
        for (; i < s.length; i++) {
          if (map.has(s[i])) {
            map.set(s[i], map.get(s[i]) - 1)
          }
          if (map.get(s[i]) == 0) {
            missingType--
          }
          //缩放区间
          while (missingType == 0) {
            if (minLength > i - left + 1) {
              minLength = i - left + 1
              result = s.slice(left, i + 1)
            }
            if (map.has(s[left])) {
              map.set(s[left], map.get(s[left]) + 1)
            }
            if (map.get(s[left]) > 0) missingType++
            left++
          }
        }
        return result
      }
      minWindow('ADOBECODEBANC', 'ABC')
    </script>
  </body>
</html>
